﻿

using HalconDotNet;

namespace COVID_19_LiquidFillUI.Models
{
    public class ImageCalc
    {
        public static void AreaCalc(HObject image, HObject region, HTuple min, HTuple max,HTuple judgeArea, out HObject ho_ResultRegion, out HTuple hv_Result,out HTuple mArea)
        {
            HObject imageReduced;
            HOperatorSet.ReduceDomain(image, region, out imageReduced);
            HOperatorSet.Threshold(imageReduced, out ho_ResultRegion, min, max);
            HOperatorSet.RegionFeatures(ho_ResultRegion,"area",out mArea);
            if (mArea >= judgeArea)
            {
                hv_Result = 1;
            }
            else
            {
                hv_Result = 0;
            }
            imageReduced.Dispose();
        }
        public static void FindModel(HObject image, HObject region,HTuple thresholeMaxGray,out HObject ho_ResultRegion,out HTuple hv_ResultRow,out HTuple hv_ResultColumn)
        {
            HObject imageReduced;
            HOperatorSet.ReduceDomain(image, region, out imageReduced);
            HObject thresholdRegion;
            HOperatorSet.Threshold(imageReduced, out thresholdRegion, 0, thresholeMaxGray);
            HObject connectedRegions;
            HOperatorSet.Connection(thresholdRegion,out connectedRegions);
            HTuple Areas;
            HOperatorSet.RegionFeatures(connectedRegions,"area",out Areas);
            var maxarea = Areas.TupleMax();
            HOperatorSet.SelectShape(connectedRegions,out ho_ResultRegion, new HTuple("area"),"and", maxarea - 1, maxarea + 1);
            HOperatorSet.RegionFeatures(ho_ResultRegion, "row", out hv_ResultRow);
            HOperatorSet.RegionFeatures(ho_ResultRegion, "column", out hv_ResultColumn);
            connectedRegions.Dispose();
            thresholdRegion.Dispose();
            imageReduced.Dispose();
        }
        public static void FindCircle(HObject image, HTuple hv_Row, HTuple hv_Column, HTuple hv_Radius, HTuple MeasureThrehold,
            out HTuple hv_ResutlRow,out HTuple hv_ResultColumn,out HTuple hv_ResultRadius)
        {
            HTuple hv_MetrologyHandle, hv_Index, hv_CircleParameter;
            HTuple hv_Width = null, hv_Height = null;
            HOperatorSet.GetImageSize(image, out hv_Width, out hv_Height);
            //创建模型
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);
            HOperatorSet.SetMetrologyModelImageSize(hv_MetrologyHandle, hv_Width, hv_Height);

            HOperatorSet.AddMetrologyObjectCircleMeasure(hv_MetrologyHandle, hv_Row, hv_Column,
                hv_Radius, 20, 5, 1, MeasureThrehold, new HTuple(), new HTuple(), out hv_Index);
            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "num_instances",
                1);
            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "measure_transition",
                "negative");
            HOperatorSet.SetMetrologyObjectParam(hv_MetrologyHandle, hv_Index, "min_score",
                0.5);
            HOperatorSet.ApplyMetrologyModel(image, hv_MetrologyHandle);

            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, hv_Index, "all", "result_type",
                "all_param", out hv_CircleParameter);
            HOperatorSet.ClearMetrologyModel(hv_MetrologyHandle);
            if (hv_CircleParameter.TupleLength() == 3)
            {
                hv_ResutlRow = hv_CircleParameter.TupleSelect(0);
                hv_ResultColumn = hv_CircleParameter.TupleSelect(1);
                hv_ResultRadius = hv_CircleParameter.TupleSelect(2);
            }
            else
            {
                hv_ResutlRow = null;
                hv_ResultColumn = null;
                hv_ResultRadius = null;
            }
        }
    }
}
